package org.chibandroid.android.map;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import android.os.Bundle;

public class AndroidMapMyLocationView extends MapActivity{

	private static final String TAG = "AndroidMapMyLocation";
	private MapView mapView;
	private MyLocationOverlay overlay;

	// 地図の初期値
	private MapController controller;
	private static final int INITIAL_ZOOM_LEVE = 18;
	private static final int INITIAL_LATITUDE  = 35853498;
	private static final int INITIAL_LONGITUDE = 139917804;



	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.mylocationmap);

		mapView = (MapView)findViewById(R.id.mapview);
		mapView.setEnabled(true);
		mapView.setClickable(true);
		mapView.setBuiltInZoomControls(true);
		mapView.setSatellite(false);

        // 位置とズームレベルの初期状態を設定する
        controller = mapView.getController();
        GeoPoint point = new GeoPoint(INITIAL_LATITUDE, INITIAL_LONGITUDE);
        controller.setCenter(point);
        controller.setZoom(INITIAL_ZOOM_LEVE);

		// GPS取得の有効化
		overlay = new MyLocationOverlay(this, mapView);
		overlay.enableMyLocation();

		// GPS取得の開始
		overlay.runOnFirstFix(new runOnFirstFix());

		mapView.getOverlays().add(overlay);
		mapView.invalidate();
    }

	private class runOnFirstFix implements Runnable{

		@Override
		public void run() {
			mapView.getController().animateTo(overlay.getMyLocation());
		}

	}

	@Override
	protected void onDestroy() {
		overlay.disableMyLocation();
		mapView.getOverlays().remove(overlay);
		super.onDestroy();
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
}